import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-07
 * Time:10:18
 */
class Student{
    private String name="小红";
    public int age;
    public Student(){
        System.out.println("Student()");
    }
    private Student(String name,int age){
        this.name=name;
        this.age=age;
    }

}
//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}

public class Test {
    public static void main(String[] args) {
        Integer a=127;
        Integer b=127;
        Integer c=128;
        Integer d=128;
        System.out.println(a==b);
        System.out.println(c);
        System.out.println(c==d);
    }
    public static void main4(String[] args) {
        //MoreParameterReturn more =(a+b)->{return (a+b)};
        MoreParameterReturn more=(a,b)->(a+b);
    }
    public  static void reflectPrivateConstructor() {
        try {
            Class<?> classStudent = Class.forName("Student");
            //注意传入对应的参数
            Constructor<?> declaredConstructorStudent = classStudent.getDeclaredConstructor(String.class, int.class);
            //Constructor<?> declaredConstructorStudent = classStudent.getConstructor();
            //设置为true后可修改访问权限
            declaredConstructorStudent.setAccessible(true);
            Object objectStudent = declaredConstructorStudent.newInstance("小明", 18);
            //Object objectStudent = declaredConstructorStudent.newInstance();
            Student student = (Student) objectStudent;
            System.out.println("获得私有构造哈数且修改姓名和年龄：" + student);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    //属性
    public static void reflectPrivateField() {
        try {
            Class<?> classStudent = Class.forName("Student");
            Field field = classStudent.getDeclaredField("age");
            field.setAccessible(true);
            //可以修改该属性的值
            Object objectStudent = classStudent.newInstance();
            Student student = (Student) objectStudent;
            field.set(student,2);
            int age = (int) field.get(student);
            System.out.println("反射私有属性修改了name："+ age);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main3(String[] args) {
        reflectPrivateField();
    }

    public  static void reflectPrivateConstructor1() {
        try {
            Class<?> a=Class.forName("Student");
            Constructor<?> constructor= a.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Object object=constructor.newInstance("a",2);
            System.out.println(object);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

        public static void main2(String[] args) {
        //第一种方法获取类
        Student student=new Student();
        Class<? extends Student> c=student.getClass();
        //第二种方法
        Class b=Student.class;
        //第三种方法
        try {
            Class<?> a=Class.forName("Student");
            Constructor<?> constructor= a.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Object object=constructor.newInstance("a",2);
            System.out.println(object);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
                 InvocationTargetException e) {
            throw new RuntimeException(e);
        }

    }
    public static void main1(String[] args) {
        int n=19;
        n |=n>>>1;
        System.out.println(n);
    }
}
